package tour.general.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tour.general.R;
import tour.general.stage.StageInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StageDetailGalleryAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private JSONArray mStageArray;
	private String mDescrition;
	private ImageButton mInfoButton;
	private String mTappa;

	public StageDetailGalleryAdapter(Context context, JSONArray stageArray) {
		mContext = context;
		mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		DisplayMetrics metrics = new DisplayMetrics();
		((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
		mStageArray = stageArray;

		// mPlaceholder = ((BitmapDrawable)
		// mContext.getResources().getDrawable(R.drawable.placeholder_img)).getBitmap();
	}

	public int getCount() {
		if (mStageArray != null) {
			return mStageArray.length();
		}
		return 0;
	}

	public Object getItem(int position) {

		if (mStageArray != null) {
			try {
				return mStageArray.get(position);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		LinearLayout view = (LinearLayout) convertView;

		if (view == null) {
			view = (LinearLayout) mLayoutInflater.inflate(R.layout.stage_detail_item, null);
		}

		JSONObject o = null;
		try {
			o = (JSONObject) mStageArray.get(position);

		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		String tappaStage = "";
		String nomeStage = "";
		String lunghezzaStage = "";
		String tipoStage = "";
		String altimetriaStage = "";
		String descritionStage = "";
		String dataStage = "";

		try {
			tappaStage = o.getString("tappa");
			nomeStage = o.getString("nome");
			lunghezzaStage = o.getString("lunghezza");
			tipoStage = o.getString("tipo");
			altimetriaStage = o.getString("altimetria");
			descritionStage = o.getString("descrizione");
			dataStage = o.getString("data");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		mDescrition = descritionStage;
		mTappa = tappaStage;
		if (tipoStage != "r") {
			TextView itemNumberText = (TextView) view.findViewById(R.id.stage_detail_item_text);
			if (!tappaStage.equals("0")) {
				itemNumberText.setText(mContext.getResources().getString(R.string.Stage_Lowercase) + " " + tappaStage);
			} else {
				itemNumberText.setText(mContext.getResources().getString(R.string.Stage_Prologue_lowercase));

			}
			TextView itemNameText = (TextView) view.findViewById(R.id.stage_detail_item_name_text);
			itemNameText.setText(nomeStage);

			TextView itemKmText = (TextView) view.findViewById(R.id.stage_detail_item_detail_text);
			itemKmText.setText(mContext.getResources().getString(R.string.Length_Lowercase) + ": " + lunghezzaStage
					+ " km");

			TextView itemDescText = (TextView) view.findViewById(R.id.stage_text_item);
			itemDescText.setText(descritionStage);

			TextView itemDataText = (TextView) view.findViewById(R.id.stage_detail_item_data_text);
			itemDataText.setText(dataStage);

			int nameResourceID = mContext.getResources().getIdentifier(altimetriaStage, "drawable",
					mContext.getApplicationInfo().packageName);

			ImageView imageView = (ImageView) view.findViewById(R.id.stage_detail_item_image);
			// imageView.setImageResource(nameResourceID);

			Options options = new Options();
			options.inSampleSize = 2;
			imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), nameResourceID, options));

		} else {
			TextView itemNumberText = (TextView) view.findViewById(R.id.stage_detail_item_text);
			itemNumberText.setText("Riposo");

			TextView itemNameText = (TextView) view.findViewById(R.id.stage_detail_item_name_text);
			itemNameText.setText("");

			TextView itemKmText = (TextView) view.findViewById(R.id.stage_detail_item_detail_text);
			itemKmText.setText("");

		}

		mInfoButton = (ImageButton) view.findViewById(R.id.info_button);

		mInfoButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Intent intent = new Intent(mContext, StageInfo.class);
				intent.putExtra("description", mDescrition);
				intent.putExtra("tappa", mTappa);
				mContext.startActivity(intent);

			}
		});

		return view;
	}

}
